#!/bin/bash

apt-get  install -y wget yum-utils device-mapper-persistent-data lvm2

#设定软件源为阿里镜像中心

#ubuntu配置
sed -i 's/http:\/\/archive.ubuntu.com/https:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list
apt-get clean all
apt-get update

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce


#关闭防火墙
systemctl stop firewalld.service

#禁用防火墙
systemctl disable firewalld.service

# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

# 关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

# 在master添加hosts
echo -e 192.168.31.220     al31220  >> /etc/hosts
echo -e 192.168.31.221     al31221  >> /etc/hosts
echo -e 192.168.31.222     al31222  >> /etc/hosts
echo -e 192.168.31.223     al31223  >> /etc/hosts


# 将桥接的IPv4流量传递到iptables的链
echo -e  net.bridge.bridge-nf-call-ip6tables = 1 > /etc/sysctl.d/k8s.conf
echo -e  net.bridge.bridge-nf-call-iptables = 1 >> /etc/sysctl.d/k8s.conf

# 生效
sysctl --system

echo vm.max_map_count=262144 >>/etc/sysctl.conf


cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
EOF
setenforce 0

apt-get remove -y kubeadm kubectl kubelet kubernetes-cni cri-tools socat  git docker  docker-ce docker-ce-cli containerd.io docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
apt-get install -y  initscripts telnet net-tools bash-completion git vim expect rsync curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc  gcc-c++  perl-ExtUtils-MakeMaker  automake autoconf libtool make dnf-utils device-mapper-persistent-data lvm2 docker-ce kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet


#配置docker阿里镜像源加速
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://3mzam45g.mirror.aliyuncs.com",
        "https://dockerhub.azk8s.cn"

    ],
    "hosts": [
        "tcp://0.0.0.0:2375",
        "unix:///var/run/docker.sock"
    ],
    "exec-opts":[
        "native.cgroupdriver=systemd"
    ]
}
EOF

mkdir -p /etc/systemd/system/docker.service.d
echo [Service] >/etc/systemd/system/docker.service.d/docker.conf
echo ExecStart= >>/etc/systemd/system/docker.service.d/docker.conf
echo ExecStart=/usr/bin/dockerd >>/etc/systemd/system/docker.service.d/docker.conf


#刷新配置
systemctl daemon-reload
systemctl enable docker
systemctl restart docker
systemctl enable kubelet
systemctl start kubelet

#将jack用户添加至docker守护者进程
gpasswd -a zzhao docker